<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Middleware;

use App\Exceptions\ApiException;
use App\Services\UserService;
use Closure;
use Illuminate\Http\Request;

class CheckLogin
{
    /**
     * Handle an incoming request.
     *
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        // return $next($request);
        $userId = (int)$request->input('user_id', null);
        $token = $request->input('token');
        if (! $userId || ! $token) {
            throw new ApiException('未登陆', config('error_code.no_login'));
        }
        $userService = new UserService();
        if ($userService->checkLogin($userId, $token)) {
            return $next($request);
        }
        throw new ApiException('未登陆', config('error_code.no_login'));
    }
}
